/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addToSklad.java
 *
 * Created on Jan 5, 2009, 4:34:54 PM
 */

package GUI;

import Dictionary.Dictionary;
import GUI.other.fastDistrAdd;
import GUI.other.fastItemAdd;
import GUI.other.fastSkladAdd;
import functions.Distributors;
import functions.Dogy;
import functions.Drugs;
import functions.Items;
import functions.Sklads;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import model.Distributor;
import model.Drug;
import model.Item;
import model.Sklad;
import stuff.FDate;

/**
 *
 * @author JArik
 */
public class AddToSklad extends javax.swing.JFrame implements GUI.other.fastAddInterface {

    /** Creates new form addToSklad */
    private MainFrame frame;
    private JTextField activeField;
    private Item choosenItem, itemToEdit;
    private Distributor choosenDistr;
    private String nameText, distText,skladText;
    private String addNewText;
    private Sklad choosenSklad;
    private GUI.Sklad skladForm;
    public AddToSklad(MainFrame nFrame, Item toEdit, GUI.Sklad skladF) {
        initComponents();
        currencyComboBox.setModel(new javax.swing.DefaultComboBoxModel(Dogy.CURRENCY));
        frame = nFrame;
        skladForm = skladF;
        choosenItem = null;
        choosenDistr = null;
        itemToEdit = toEdit;

        inDateField.setText(new FDate(Calendar.getInstance().getTime().getTime())+"");
        distBox.setForeground(Color.gray);
        skladBox.setForeground(Color.gray);
        try {
            ArrayList types = Drugs.getTypes();
            types.add(0,Dictionary.ALL);
            types.add(1,Dictionary.DRUGS);
            types.add(2,Dictionary.OTHER);
            typeBox.setModel(new javax.swing.DefaultComboBoxModel(types.toArray()));
            typeBox.setSelectedIndex(1);
            taxField.setText(Dogy.DRUG_TAX+"");
        } catch (SQLException ex) {
            frame.SQLException(ex);
        }

        nameText = Dictionary.ENTER_ITEM_OR_DRUG;
        distText = Dictionary.DISTRIBUTOR;
        skladText = Dictionary.WAREHOUSE;
        addNewText = "<html><b>"+Dictionary.ADD_NEW+"</b>";
        boxPopup.add(boxList);
        Dimension dim = list.getSize();
        dim.width+=15;
        list.setMinimumSize(dim);

        add1Field.setText("0");
        add2Field.setText("0");
        add3Field.setText("0");
        cena1Field.setText("0");
        cena2Field.setText("0");
        cena3Field.setText("0");
        nettoField.setText("0");
        Dogy.placeToCenter(this);

        if(itemToEdit != null)
          fillForms();

        KeyStroke keyStr = KeyStroke.getKeyStroke(KeyEvent.VK_W,2);
        InputMap map = descrTextField.getInputMap(javax.swing.JTable.WHEN_IN_FOCUSED_WINDOW);
        map.put(keyStr, "ctrlW");
        ActionMap actionMap = descrTextField.getActionMap();
        actionMap.put("ctrlW", new AbstractAction() {
                     public void actionPerformed(ActionEvent actionEvent)
                     {
                         jButton2ActionPerformed(null);
                     }
             });
    }

    private void fillForms()
    {
        choosenItem = itemToEdit;
        choosenSklad = itemToEdit.getSklad();
        currencyComboBox.setSelectedItem(itemToEdit.getCurrency());
        addButton.setText(Dictionary.CHANGE);
        nameBox.setText(itemToEdit+"");
        nameBox.setForeground(Color.black);
        skladBox.setText(itemToEdit.getSklad() +"");
        skladBox.setForeground(Color.black);
        descrTextField.setText(itemToEdit.getDescr());
        if(itemToEdit.getInDate() != null)
          inDateField.setText(new FDate(itemToEdit.getInDate().getTime())+"");
        else
          inDateField.setText("");
        
        if(itemToEdit.getDist() != null)
        {
          distBox.setText(itemToEdit.getDist()+"");
          distBox.setForeground(Color.black);
          choosenDistr = itemToEdit.getDist();
        }
        nettoField.setText(itemToEdit.getBuyPrice()+"");
        amountField.setText(itemToEdit.getAmount()+"");
        taxField.setText(itemToEdit.getTax()+"");
        cena1Field.setText(itemToEdit.getSellPrices()[0]+"");
        cena2Field.setText(itemToEdit.getSellPrices()[1]+"");
        cena3Field.setText(itemToEdit.getSellPrices()[2]+"");

        add1Field.setText(Dogy.roundTwoDecimals(itemToEdit.getSellPrices()[0] -
                          itemToEdit.getSellPrices()[0]*(itemToEdit.getTax()/100) -
                          itemToEdit.getBuyPrice())+"");
        add2Field.setText(Dogy.roundTwoDecimals(itemToEdit.getSellPrices()[1] -
                          itemToEdit.getSellPrices()[1]*(itemToEdit.getTax()/100) -
                          itemToEdit.getBuyPrice())+"");
        add3Field.setText(Dogy.roundTwoDecimals(itemToEdit.getSellPrices()[2] -
                          Dogy.roundPrice(itemToEdit.getSellPrices()[2]*(itemToEdit.getTax()/100)) -
                          itemToEdit.getBuyPrice())+"");

        calcFromBrutto();
    }

    private void calculateSums() {
        double netto = Double.parseDouble(nettoField.getText());
        double add1 = Double.parseDouble(add1Field.getText());
        double add2 = Double.parseDouble(add2Field.getText());
        double add3 = Double.parseDouble(add3Field.getText());

        double tax = Double.parseDouble(taxField.getText().replace('%', ' ').trim());
        
        cena1Field.setText( Dogy.roundPrice((netto+add1)/(1 - tax/100)) +"");
        cena2Field.setText( Dogy.roundPrice((netto+add2)/(1 - tax/100)) +"");
        cena3Field.setText( Dogy.roundPrice((netto+add3)/(1 - tax/100)) +"");

        
        // X*(1-tax)=des
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        boxPopup = new javax.swing.JPopupMenu();
        boxList = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        dialog = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        typeBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        nettoField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        add1Field = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cena1Field = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        amountField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descrTextField = new javax.swing.JTextArea();
        addButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        nameBox = new javax.swing.JTextField();
        distBox = new javax.swing.JTextField();
        add2Field = new javax.swing.JTextField();
        add3Field = new javax.swing.JTextField();
        cena2Field = new javax.swing.JTextField();
        cena3Field = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        allField = new javax.swing.JTextField();
        skladBox = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        procAddField1 = new javax.swing.JTextField();
        procAddField2 = new javax.swing.JTextField();
        procAddField3 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        inDateField = new javax.swing.JFormattedTextField();
        jLabel14 = new javax.swing.JLabel();
        taxField = new javax.swing.JFormattedTextField();
        jLabel15 = new javax.swing.JLabel();
        currencyComboBox = new javax.swing.JComboBox();

        boxPopup.setAutoscrolls(true);
        boxPopup.setMaximumSize(new java.awt.Dimension(290, 645555));
        boxPopup.setPreferredSize(new java.awt.Dimension(290, 120));

        boxList.setAutoscrolls(true);
        boxList.setMaximumSize(new java.awt.Dimension(290, 32767));
        boxList.setMinimumSize(new java.awt.Dimension(120, 23));

        list.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Добавить новое" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        list.setMaximumSize(new java.awt.Dimension(290, 16));
        list.setNextFocusableComponent(distBox);
        list.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listMouseClicked(evt);
            }
        });
        boxList.setViewportView(list);

        dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        dialog.setAlwaysOnTop(true);
        dialog.setResizable(false);
        dialog.setUndecorated(true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                dialogWindowClosed(evt);
            }
        });

        javax.swing.GroupLayout dialogLayout = new javax.swing.GroupLayout(dialog.getContentPane());
        dialog.getContentPane().setLayout(dialogLayout);
        dialogLayout.setHorizontalGroup(
            dialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        dialogLayout.setVerticalGroup(
            dialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jLabel4.setText("jLabel4");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Добавление на склад");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        typeBox.setFont(new java.awt.Font("Tahoma", 0, 14));
        typeBox.setModel(new javax.swing.DefaultComboBoxModel(
            new String[] { "Все лек.", Dictionary.OTHER, "Добавить тип" }));
    typeBox.setToolTipText("Искать по типу");
    typeBox.setAutoscrolls(true);
    typeBox.setRequestFocusEnabled(false);
    typeBox.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            typeBoxItemStateChanged(evt);
        }
    });
    getContentPane().add(typeBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 30, 100, 23));

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel1.setText(Dictionary.BUY_PRICE_ONE);
    getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 120, 130, -1));

    nettoField.setFont(new java.awt.Font("Tahoma", 0, 14));
    nettoField.setNextFocusableComponent(cena1Field);
    nettoField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nettoFieldActionPerformed(evt);
        }
    });
    nettoField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            nettoFieldFocusLost(evt);
        }
    });
    nettoField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            nettoFieldKeyReleased(evt);
        }
    });
    getContentPane().add(nettoField, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 140, 120, -1));

    jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
    jLabel2.setText(Dictionary.ADD_TO_WAREHOUSE);
    getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, 230, -1));

    add1Field.setFont(new java.awt.Font("Tahoma", 0, 14));
    add1Field.setToolTipText("");
    add1Field.setNextFocusableComponent(add2Field);
    add1Field.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            add1FieldActionPerformed(evt);
        }
    });
    add1Field.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            add1FieldFocusLost(evt);
        }
    });
    add1Field.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            add1FieldKeyReleased(evt);
        }
    });
    getContentPane().add(add1Field, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 200, 130, -1));

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel3.setText(Dictionary.MNY_MARKUP);
    getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, 140, -1));

    cena1Field.setFont(new java.awt.Font("Tahoma", 0, 14));
    cena1Field.setToolTipText("Продажная цена за ед. товара");
    cena1Field.setNextFocusableComponent(cena2Field);
    cena1Field.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cena1FieldActionPerformed(evt);
        }
    });
    cena1Field.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            cena1FieldFocusLost(evt);
        }
    });
    cena1Field.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            cena1FieldKeyReleased(evt);
        }
    });
    getContentPane().add(cena1Field, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 200, 120, -1));

    jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    jLabel5.setText(Dictionary.SELL_PRICE_ONE);
    jLabel5.setToolTipText("Продажная цена за ед. товара");
    getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 180, 150, -1));
    getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 20, 460, 10));

    amountField.setFont(new java.awt.Font("Tahoma", 0, 14));
    amountField.setText("1");
    amountField.setNextFocusableComponent(nettoField);
    amountField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            amountFieldActionPerformed(evt);
        }
    });
    amountField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            amountFieldKeyReleased(evt);
        }
    });
    getContentPane().add(amountField, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, 130, -1));

    jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel7.setText(Dictionary.INCOME_IN_PIECE);
    getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, 130, -1));

    jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel8.setText(Dictionary.TOTAL_BUY_PRICE);
    getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 120, 150, -1));

    jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(Dictionary.DESCR_OF_LOT));

    descrTextField.setColumns(20);
    descrTextField.setFont(new java.awt.Font("Verdana", 0, 15));
    descrTextField.setRows(3);
    descrTextField.setBorder(null);
    descrTextField.setNextFocusableComponent(addButton);
    jScrollPane1.setViewportView(descrTextField);

    getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 342, 450, 120));

    addButton.setFont(new java.awt.Font("Tahoma", 1, 14));
    addButton.setText(Dictionary.ADD);
    addButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addButtonActionPerformed(evt);
        }
    });
    getContentPane().add(addButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 470, 150, 50));

    jButton2.setFont(new java.awt.Font("Tahoma", 1, 14));
    jButton2.setText(Dictionary.CLOSE);
    jButton2.setFocusCycleRoot(true);
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 470, 150, 50));

    nameBox.setFont(new java.awt.Font("Tahoma", 0, 14));
    nameBox.setText(Dictionary.ENTER_ITEM_OR_DRUG);
    nameBox.setName("nameBox"); // NOI18N
    nameBox.setNextFocusableComponent(distBox);
    nameBox.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            nameBoxMouseClicked(evt);
        }
    });
    nameBox.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            nameBoxFocusGained(evt);
        }
        public void focusLost(java.awt.event.FocusEvent evt) {
            nameBoxFocusLost(evt);
        }
    });
    nameBox.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            nameBoxKeyPressed(evt);
        }
        public void keyReleased(java.awt.event.KeyEvent evt) {
            nameBoxKeyReleased(evt);
        }
    });
    getContentPane().add(nameBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 340, -1));

    distBox.setFont(new java.awt.Font("Tahoma", 0, 14));
    distBox.setText(Dictionary.DISTRIBUTOR);
    distBox.setName("distBox"); // NOI18N
    distBox.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            distBoxMouseClicked(evt);
        }
    });
    distBox.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            distBoxFocusGained(evt);
        }
        public void focusLost(java.awt.event.FocusEvent evt) {
            distBoxFocusLost(evt);
        }
    });
    distBox.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            distBoxKeyPressed(evt);
        }
        public void keyReleased(java.awt.event.KeyEvent evt) {
            distBoxKeyReleased(evt);
        }
    });
    getContentPane().add(distBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 340, -1));

    add2Field.setFont(new java.awt.Font("Tahoma", 0, 14));
    add2Field.setNextFocusableComponent(add3Field);
    add2Field.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            add2FieldActionPerformed(evt);
        }
    });
    add2Field.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            add2FieldFocusLost(evt);
        }
    });
    add2Field.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            add2FieldKeyReleased(evt);
        }
    });
    getContentPane().add(add2Field, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 230, 130, -1));

    add3Field.setFont(new java.awt.Font("Tahoma", 0, 14));
    add3Field.setNextFocusableComponent(taxField);
    add3Field.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            add3FieldActionPerformed(evt);
        }
    });
    add3Field.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            add3FieldFocusLost(evt);
        }
    });
    add3Field.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            add3FieldKeyReleased(evt);
        }
    });
    getContentPane().add(add3Field, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 260, 130, -1));

    cena2Field.setFont(new java.awt.Font("Tahoma", 0, 14));
    cena2Field.setToolTipText("Продажная цена за ед. товара");
    cena2Field.setNextFocusableComponent(cena3Field);
    cena2Field.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cena2FieldActionPerformed(evt);
        }
    });
    cena2Field.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            cena2FieldFocusLost(evt);
        }
    });
    cena2Field.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            cena2FieldKeyReleased(evt);
        }
    });
    getContentPane().add(cena2Field, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 230, 120, -1));

    cena3Field.setFont(new java.awt.Font("Tahoma", 0, 14));
    cena3Field.setToolTipText("Продажная цена за ед. товара");
    cena3Field.setNextFocusableComponent(taxField);
    cena3Field.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cena3FieldActionPerformed(evt);
        }
    });
    cena3Field.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            cena3FieldFocusLost(evt);
        }
    });
    cena3Field.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            cena3FieldKeyReleased(evt);
        }
    });
    getContentPane().add(cena3Field, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 260, 120, -1));

    jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel9.setText(Dictionary.DATE_IN);
    getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 290, 120, -1));

    allField.setEditable(false);
    allField.setFont(new java.awt.Font("Tahoma", 0, 14));
    allField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            allFieldActionPerformed(evt);
        }
    });
    getContentPane().add(allField, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 140, 120, -1));

    skladBox.setFont(new java.awt.Font("Tahoma", 0, 14));
    skladBox.setText(Dictionary.WAREHOUSE);
    skladBox.setName("skladBox"); // NOI18N
    skladBox.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            skladBoxMouseClicked(evt);
        }
    });
    skladBox.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            skladBoxFocusGained(evt);
        }
        public void focusLost(java.awt.event.FocusEvent evt) {
            skladBoxFocusLost(evt);
        }
    });
    skladBox.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            skladBoxKeyPressed(evt);
        }
        public void keyReleased(java.awt.event.KeyEvent evt) {
            skladBoxKeyReleased(evt);
        }
    });
    getContentPane().add(skladBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, 340, -1));

    jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel6.setText(Dictionary.PERC_MARKUP);
    getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 180, 130, -1));

    procAddField1.setFont(new java.awt.Font("Tahoma", 0, 14));
    procAddField1.setText("0");
    procAddField1.setNextFocusableComponent(procAddField2);
    procAddField1.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            procAddField1FocusLost(evt);
        }
    });
    procAddField1.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            procAddField1KeyReleased(evt);
        }
    });
    getContentPane().add(procAddField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 200, 120, -1));

    procAddField2.setFont(new java.awt.Font("Tahoma", 0, 14));
    procAddField2.setText("0");
    procAddField2.setNextFocusableComponent(procAddField3);
    procAddField2.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            procAddField2FocusLost(evt);
        }
    });
    procAddField2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            procAddField2KeyReleased(evt);
        }
    });
    getContentPane().add(procAddField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 230, 120, -1));

    procAddField3.setFont(new java.awt.Font("Tahoma", 0, 14));
    procAddField3.setText("0");
    procAddField3.setNextFocusableComponent(taxField);
    procAddField3.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            procAddField3FocusLost(evt);
        }
    });
    procAddField3.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            procAddField3KeyReleased(evt);
        }
    });
    getContentPane().add(procAddField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 260, 120, -1));

    jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel10.setText("%");
    getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 230, -1, 20));

    jLabel11.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    jLabel11.setText("%");
    getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 310, -1, 20));

    jLabel12.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel12.setText("%");
    getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 200, -1, 20));

    jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel13.setText("%");
    getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 260, -1, 20));

    inDateField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd.MM.yyyy"))));
    inDateField.setFont(new java.awt.Font("Tahoma", 0, 14));
    getContentPane().add(inDateField, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 310, 120, -1));

    jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel14.setText("Валюта");
    getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 290, 110, -1));

    taxField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("##.#"))));
    taxField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    taxField.setNextFocusableComponent(addButton);
    taxField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            taxFieldKeyReleased(evt);
        }
    });
    getContentPane().add(taxField, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 310, 120, -1));

    jLabel15.setFont(new java.awt.Font("Tahoma", 1, 14));
    jLabel15.setText(Dictionary.TAX+"(НДС)");
    getContentPane().add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 290, 130, -1));

    currencyComboBox.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    currencyComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "EEK", "EUR" }));
    getContentPane().add(currencyComboBox, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 310, 70, -1));

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nettoFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nettoFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_nettoFieldActionPerformed

    private void add1FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add1FieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_add1FieldActionPerformed

    private void cena1FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cena1FieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cena1FieldActionPerformed

    private void amountFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_amountFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_amountFieldActionPerformed

    private void typeBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_typeBoxItemStateChanged
        nameBoxKeyReleased(null);
        list.setSelectedIndex(list.getFirstVisibleIndex()+1);
    }//GEN-LAST:event_typeBoxItemStateChanged

    private void nameBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameBoxFocusLost
        boxPopup.setVisible(false);
        Dogy.focusLost(nameBox, Dictionary.ENTER_ITEM_OR_DRUG);
               if(nameBoxIsEmpty())
                   choosenItem = null;
    }//GEN-LAST:event_nameBoxFocusLost

    private void nameBoxFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameBoxFocusGained
        activeField = nameBox;
        Dogy.focusGained(nameBox, Dictionary.ENTER_ITEM_OR_DRUG);
        if(nameBoxIsEmpty())
            nameBoxKeyReleased(null);


    }//GEN-LAST:event_nameBoxFocusGained

    private void nameBoxKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameBoxKeyReleased
        activeField = nameBox;
        if( evt != null && evt.getKeyCode() == KeyEvent.VK_ENTER) {
            boxPopup.setVisible(false);
            
            if(!nameBox.getText().equals("")|| list.getSelectedIndex() >0)
            {
               listMouseClicked(null);
               nameBox.nextFocus();
            }
            else
                showAddWindow();
            list.setSelectedValue(null, false);
        } else if(evt != null && evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            boxPopup.setVisible(false);
        }
        else if(!dialog.isVisible() && (evt == null || !evt.isActionKey()) ){
            try {
                list.ensureIndexIsVisible(0);
                ArrayList choices=null;
                if(typeBox.getSelectedIndex() == 0) {
                    choices= Drugs.getByName(nameBox.getText());
                    choices.addAll(Items.getItemByName(nameBox.getText()));
                } else if(typeBox.getSelectedIndex() == 1) {
                    choices= Drugs.getByName(nameBox.getText());
                } else if (typeBox.getSelectedItem().toString().equals(Dictionary.OTHER)) {
                    choices = Items.getItemByName(nameBox.getText());
                } else {
                    choices= Drugs.getByNameAndType(nameBox.getText(),typeBox.getSelectedItem().toString());
                }


                choices.add(0,addNewText);


                list.setListData(choices.toArray());
                try
                {
                boxPopup.setLocation(nameBox.getLocationOnScreen().x,
                        nameBox.getLocationOnScreen().y + nameBox.getHeight());
                list.repaint();
                boxPopup.repaint();
                boxPopup.setVisible(true);
                }catch (Exception e) { }
                

            } catch (SQLException ex) {
                frame.SQLException(ex);
            }
        }
    }//GEN-LAST:event_nameBoxKeyReleased

    private void nameBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nameBoxMouseClicked
        activeField = nameBox;
        if(nameBox.getText().equals(""))
            nameBoxKeyReleased(null);
    }//GEN-LAST:event_nameBoxMouseClicked

    private void listMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked
        boxPopup.setVisible(false);
        if ( list.getSelectedValue() != null &&
                list.getSelectedValue().toString().equals(addNewText) ||
                list.getSelectedIndex() == -1)
        {
            showAddWindow();
        } else
        {
            if(activeField != null)
            {
               JTextField field = activeField;
               field.setText(list.getSelectedValue().toString());

               if(activeField.getName().equals("nameBox"))
                    choosenItem = (Item)list.getSelectedValue();
               if(activeField.getName().equals("distBox"))
                    choosenDistr = (Distributor)list.getSelectedValue();
               if(activeField.getName().equals("skladBox"))
                    choosenSklad = (Sklad)list.getSelectedValue();

            }
            list.setSelectedIndex(0);
        }

    }//GEN-LAST:event_listMouseClicked

    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
        if(activeField != null && (boxPopup.isVisible() || dialog.isVisible()) )
        {
            JTextField field = activeField;
        boxPopup.setLocation(field.getLocationOnScreen().x,
                        field.getLocationOnScreen().y + field.getHeight());
        dialog.setLocation(field.getLocationOnScreen().x,
        field.getLocationOnScreen().y + field.getHeight());
        }
    }//GEN-LAST:event_formComponentMoved

    private void distBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_distBoxMouseClicked
        activeField = distBox;
        if(distBox.getText().equals(""))
            distBoxKeyReleased(null);
}//GEN-LAST:event_distBoxMouseClicked

    private void distBoxFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_distBoxFocusGained
        activeField = distBox;
        Dogy.focusGained(distBox, distText);
        if(distBoxIsEmpty())
            distBoxKeyReleased(null);
}//GEN-LAST:event_distBoxFocusGained

    private void distBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_distBoxFocusLost
        boxPopup.setVisible(false);
        Dogy.focusLost(distBox, distText);
        if(distBoxIsEmpty())
                   choosenDistr = null;
}//GEN-LAST:event_distBoxFocusLost

    private void distBoxKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_distBoxKeyReleased
        activeField = distBox;
        if( evt != null && evt.getKeyCode() == KeyEvent.VK_ENTER) {
            boxPopup.setVisible(false);
            if(!distBox.getText().equals("") || list.getSelectedIndex() >0)
            {
               listMouseClicked(null);
               distBox.nextFocus();
            }
            else
                showAddWindow();
            
        } else if(evt != null && evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            boxPopup.setVisible(false);
        }
        else if(!dialog.isVisible() && (evt == null || !evt.isActionKey()) ){
            try {
                list.ensureIndexIsVisible(0);
                ArrayList choices=null;
                if( choosenItem != null && choosenItem instanceof Drug
                        && !nameBox.getText().equals(nameText))
                  choices= Distributors.getByName(distBox.getText());
                else if(choosenItem != null && choosenItem instanceof Item &&
                        !nameBox.getText().equals(nameText))
                  choices= Distributors.getByName(distBox.getText());
                else
                {
                    choices= Distributors.getByName(distBox.getText());
                }
                
                choices.add(0,addNewText);

                list.setListData(choices.toArray());

                boxPopup.setLocation(distBox.getLocationOnScreen().x,
                        distBox.getLocationOnScreen().y + distBox.getHeight());
                list.repaint();
                boxPopup.repaint();
                boxPopup.setVisible(true);

            } catch (SQLException ex) {
                frame.SQLException(ex);
            }
        }
}//GEN-LAST:event_distBoxKeyReleased

    private void nameBoxKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameBoxKeyPressed
           /*if(evt.getKeyCode() == evt.VK_DOWN)
            if(list.getSelectedIndex() == -1)
                list.setSelectedIndex(0);
            else
            {
              list.setSelectedIndex(list.getSelectedIndex()+1);
              int vR = (list.getLastVisibleIndex() - list.getFirstVisibleIndex())/2;
              list.ensureIndexIsVisible(list.getSelectedIndex()+ vR);
            }
            if(evt.getKeyCode() == evt.VK_UP)
            {
                if(list.getSelectedIndex() >0)
                {
                  list.setSelectedIndex(list.getSelectedIndex()-1);
                  int vR = (list.getLastVisibleIndex() - list.getFirstVisibleIndex())/2;
                  int vis = list.getSelectedIndex()- vR;
                  if (vis < 0) vis = 0;
                  list.ensureIndexIsVisible(vis);
                }
            }*/
           Dogy.listScroll(list, evt);
    }//GEN-LAST:event_nameBoxKeyPressed

    private void distBoxKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_distBoxKeyPressed
           /*if(evt.getKeyCode() == evt.VK_DOWN)
            {
                list.setSelectedIndex(list.getSelectedIndex()+1);
                int vR = (list.getLastVisibleIndex() - list.getFirstVisibleIndex())/2;
                list.ensureIndexIsVisible(list.getSelectedIndex()+ vR);
            }
            if(evt.getKeyCode() == evt.VK_UP)
            {
               if(list.getSelectedIndex() >0)
               {
                list.setSelectedIndex(list.getSelectedIndex()-1);
                int vR = (list.getLastVisibleIndex() - list.getFirstVisibleIndex())/2;
                int vis = list.getSelectedIndex()- vR;
                if (vis < 0) vis = 0;
                list.ensureIndexIsVisible(vis);
               }
            }*/
        Dogy.listScroll(list, evt);
    }//GEN-LAST:event_distBoxKeyPressed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed

        if(itemToEdit != null)
            choosenItem = itemToEdit;
        
        if (choosenDistr != null &&  choosenDistr.getId() == -1)
        {
            try {
                int id = -1;
                if (choosenItem != null && choosenItem instanceof Drug) {
                    id = Distributors.addToCatalog(choosenDistr);
                } else if (choosenItem != null &&choosenItem instanceof Item) {
                    id = Distributors.addOtherToCatalog(choosenDistr);
                }else
                    throw new SQLException("Нужно выбрать лекарство или предмет");
                choosenDistr.setId(id);
            } catch (SQLException ex) {
                frame.SQLException(ex);
            }
        }        
        
        double netto = Double.parseDouble(nettoField.getText());
        double add1 = Double.parseDouble(add1Field.getText());
        double add2 = Double.parseDouble(add2Field.getText());
        double add3 = Double.parseDouble(add3Field.getText());

        double 
                cena1 = Double.parseDouble(cena1Field.getText()),
                cena2 = Double.parseDouble(cena2Field.getText()),
                cena3 = Double.parseDouble(cena3Field.getText());

        double amount = Double.parseDouble(amountField.getText());
        String currency = currencyComboBox.getSelectedItem().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        try
        {
            choosenItem.setInDate(new FDate(sdf.parse(inDateField.getText()).getTime()));
        } catch(Exception e)
        {
            choosenItem.setInDate(null);
        }
        choosenItem.setSellPrices(cena1, cena2, cena3);
        choosenItem.setBuyPrice(netto);
        choosenItem.setAmount(amount);
        choosenItem.setDescr(descrTextField.getText());
        choosenItem.setDistributor(choosenDistr);
        
        try
        {
          if(itemToEdit == null)
          {
            Drugs.addToSklad(choosenItem, amount, currency, choosenSklad.getId(),
                             Double.parseDouble(taxField.getText()));
          }
          else
          {
            Drugs.updateSklad(choosenItem, amount, currency, choosenSklad.getId(),
                             Double.parseDouble(taxField.getText()));
          }
          skladForm.refreshSearch();
          this.dispose();
        } catch(SQLException e)
        {
          frame.SQLException(e);
        }
    }//GEN-LAST:event_addButtonActionPerformed

    private void dialogWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_dialogWindowClosed
        this.setEnabled(true);
        this.requestFocus();
    }//GEN-LAST:event_dialogWindowClosed

    private void add2FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add2FieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_add2FieldActionPerformed

    private void add3FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_add3FieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_add3FieldActionPerformed

    private void cena2FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cena2FieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cena2FieldActionPerformed

    private void cena3FieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cena3FieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cena3FieldActionPerformed

    private void allFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_allFieldActionPerformed

    private void nettoFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nettoFieldKeyReleased
        calculateSums();
        calcFromBrutto();
        double amount = Double.parseDouble(amountField.getText());
        allField.setText(Dogy.roundPrice(Double.parseDouble(nettoField.getText())*amount) + "");
    }//GEN-LAST:event_nettoFieldKeyReleased

    private void add1FieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_add1FieldFocusLost
        if(add1Field.getText().equals(""))
            add1Field.setText("0");
    }//GEN-LAST:event_add1FieldFocusLost

    private void add2FieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_add2FieldFocusLost
        if(add2Field.getText().equals(""))
            add2Field.setText("0");
    }//GEN-LAST:event_add2FieldFocusLost

    private void add3FieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_add3FieldFocusLost
        if(add3Field.getText().equals(""))
            add3Field.setText("0");
    }//GEN-LAST:event_add3FieldFocusLost

    private void nettoFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nettoFieldFocusLost
      if(nettoField.getText().equals(""))
            nettoField.setText("0");
    }//GEN-LAST:event_nettoFieldFocusLost

    private void add1FieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_add1FieldKeyReleased
        calculateSums();
    }//GEN-LAST:event_add1FieldKeyReleased

    private void add2FieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_add2FieldKeyReleased
        calculateSums();
    }//GEN-LAST:event_add2FieldKeyReleased

    private void add3FieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_add3FieldKeyReleased
        calculateSums();
    }//GEN-LAST:event_add3FieldKeyReleased

    private void amountFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_amountFieldKeyReleased
        double amount = Double.parseDouble(amountField.getText());
        allField.setText(Dogy.roundPrice(Double.parseDouble(nettoField.getText())*amount) + "");
    }//GEN-LAST:event_amountFieldKeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(boxPopup.isVisible())
          boxPopup.setVisible(false);
       // dialog.dispose();
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void skladBoxMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_skladBoxMouseClicked
         activeField = skladBox;
        if(skladBox.getText().equals(""))
            skladBoxKeyReleased(null);
}//GEN-LAST:event_skladBoxMouseClicked

    private void skladBoxFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_skladBoxFocusGained
        activeField = skladBox;
        Dogy.focusGained(skladBox, skladText);
        if(skladBoxIsEmpty())
            skladBoxKeyReleased(null);
}//GEN-LAST:event_skladBoxFocusGained

    private void skladBoxFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_skladBoxFocusLost
        boxPopup.setVisible(false);
        Dogy.focusLost(skladBox, skladText);
        if(skladBoxIsEmpty())
           choosenSklad = null;
}//GEN-LAST:event_skladBoxFocusLost

    private void skladBoxKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_skladBoxKeyPressed
        Dogy.listScroll(list, evt);
}//GEN-LAST:event_skladBoxKeyPressed

    private void skladBoxKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_skladBoxKeyReleased
        activeField = skladBox;
        if( evt != null && evt.getKeyCode() == KeyEvent.VK_ENTER) {
            boxPopup.setVisible(false);
            
            if(!skladBox.getText().equals("") || list.getSelectedIndex() >0)
            {
               listMouseClicked(null);
               skladBox.nextFocus();
            }
            else
                showAddWindow();
            list.setSelectedIndex(-1);
        } else if(evt != null && evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            boxPopup.setVisible(false);
        }
        else if(!dialog.isVisible() && (evt == null || !evt.isActionKey()) ){
            try {
                list.ensureIndexIsVisible(0);
                ArrayList choices=null;
                choices= Sklads.getByName(skladBox.getText());

                choices.add(0,addNewText);

                list.setListData(choices.toArray());

                boxPopup.setLocation(skladBox.getLocationOnScreen().x,
                        skladBox.getLocationOnScreen().y + skladBox.getHeight());
                list.repaint();
                boxPopup.repaint();
                boxPopup.setVisible(true);

            } catch (SQLException ex) {
                frame.SQLException(ex);
            }
        }
}//GEN-LAST:event_skladBoxKeyReleased

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        boxPopup.setVisible(false);
        dialog.setVisible(false);
    }//GEN-LAST:event_formWindowClosing

    private void cena1FieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cena1FieldKeyReleased
        calcFromBrutto();
    }//GEN-LAST:event_cena1FieldKeyReleased

    private void cena2FieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cena2FieldKeyReleased
        calcFromBrutto();
    }//GEN-LAST:event_cena2FieldKeyReleased

    private void cena3FieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cena3FieldKeyReleased
        calcFromBrutto();
    }//GEN-LAST:event_cena3FieldKeyReleased

    private void cena1FieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cena1FieldFocusLost
        if(cena1Field.getText().isEmpty())
            cena1Field.setText("0");
    }//GEN-LAST:event_cena1FieldFocusLost

    private void cena2FieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cena2FieldFocusLost
        if(cena2Field.getText().isEmpty())
            cena2Field.setText("0");
    }//GEN-LAST:event_cena2FieldFocusLost

    private void cena3FieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cena3FieldFocusLost
        if(cena3Field.getText().isEmpty())
            cena3Field.setText("0");
    }//GEN-LAST:event_cena3FieldFocusLost

    private void procAddField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_procAddField1KeyReleased
        calcFromProc();
    }//GEN-LAST:event_procAddField1KeyReleased

    private void procAddField2KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_procAddField2KeyReleased
        calcFromProc();
    }//GEN-LAST:event_procAddField2KeyReleased

    private void procAddField3KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_procAddField3KeyReleased
        calcFromProc();
    }//GEN-LAST:event_procAddField3KeyReleased

    private void procAddField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_procAddField1FocusLost
        if(procAddField1.getText().isEmpty())
            procAddField1.setText("0");
    }//GEN-LAST:event_procAddField1FocusLost

    private void procAddField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_procAddField2FocusLost
        if(procAddField2.getText().isEmpty())
            procAddField2.setText("0");
    }//GEN-LAST:event_procAddField2FocusLost

    private void procAddField3FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_procAddField3FocusLost
        if(procAddField3.getText().isEmpty())
            procAddField3.setText("0");
    }//GEN-LAST:event_procAddField3FocusLost

    private void taxFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taxFieldKeyReleased
        // TODO add your handling code here:
}//GEN-LAST:event_taxFieldKeyReleased

    private void calcAddFromBrutto()
    {
        double
                cena1 = Double.parseDouble(cena1Field.getText()),
                cena2 = Double.parseDouble(cena2Field.getText()),
                cena3 = Double.parseDouble(cena3Field.getText());
        double tax = Double.parseDouble(taxField.getText());
        double netto = Double.parseDouble(nettoField.getText());
        add1Field.setText(Dogy.roundTwoDecimals(cena1 -
                          Dogy.roundPrice(cena1*(tax/100)) -
                          netto)+"");
        add2Field.setText(Dogy.roundTwoDecimals(cena2 -
                          Dogy.roundPrice(cena2*(tax/100)) -
                          netto)+"");
        add3Field.setText(Dogy.roundTwoDecimals(cena3 -
                          Dogy.roundPrice(cena3*(tax/100)) -
                          netto)+"");
    }
    private void calcFromBrutto()
    {
        double
                cena1 = Double.parseDouble(cena1Field.getText()),
                cena2 = Double.parseDouble(cena2Field.getText()),
                cena3 = Double.parseDouble(cena3Field.getText());
        double netto = Double.parseDouble(nettoField.getText());
        
        calcAddFromBrutto();
        
        procAddField1.setText(Dogy.roundTwoDecimals((cena1/netto - 1)*100)+"");
        procAddField2.setText(Dogy.roundTwoDecimals((cena2/netto - 1)*100)+"");
        procAddField3.setText(Dogy.roundTwoDecimals((cena3/netto - 1)*100)+"");
    }

    private void calcFromProc()
    {
        double netto = Double.parseDouble(nettoField.getText());
        double tax = Double.parseDouble(taxField.getText().replace('%', ' ').trim())/100;

        double  proc1 = Double.parseDouble(procAddField1.getText().replace('%', ' ').trim())/100,
                proc2 =Double.parseDouble(procAddField2.getText().replace('%', ' ').trim())/100,
                proc3 = Double.parseDouble(procAddField3.getText().replace('%', ' ').trim())/100;

        cena1Field.setText(Dogy.roundTwoDecimals(netto*(1+proc1))+"");
        cena2Field.setText(Dogy.roundTwoDecimals(netto*(1+proc2))+"");
        cena3Field.setText(Dogy.roundTwoDecimals(netto*(1+proc3))+"");

        calcAddFromBrutto();

    }
private void showAddWindow()
{
    if(activeField != null && activeField.getName().equals("nameBox"))
          dialog.setContentPane(new fastItemAdd(choosenItem,this));
    
    else if(activeField !=null && activeField.getName().equals("distBox"))
         dialog.setContentPane(new fastDistrAdd(choosenDistr,this));

    else if(activeField !=null && activeField.getName().equals("skladBox"))
         dialog.setContentPane(new fastSkladAdd(choosenSklad,this));
    
    dialog.pack();
    dialog.setLocation(activeField.getLocationOnScreen().x,
           activeField.getLocationOnScreen().y + activeField.getHeight());
    dialog.setVisible(true);
}

public void setActiveFieldText(Object obj)
{
    if(obj instanceof Item || obj instanceof Drug)
    {
      activeField.setForeground(Color.black);
      activeField.setText(obj.toString());
      choosenItem = (Item)obj;
    }
    if(obj instanceof Distributor)
    {
      activeField.setForeground(Color.black);
      activeField.setText(obj.toString());
      choosenDistr = (Distributor)obj;
    }

    if(obj instanceof Sklad)
    {
      activeField.setForeground(Color.black);
      activeField.setText(obj.toString());
      choosenSklad = (Sklad)obj;
    }
    activeField.nextFocus();

}

public void disposeDialog()
{
    dialog.setVisible(false);
    //dialog.dispose();
}
public Item getChoosenItem()
{
    return choosenItem;
}
public String getActiveFieldText()
{
    return activeField.getText();
}
private boolean nameBoxIsEmpty()
{
    return nameBox.getText().equals("") || nameBox.getText().equals(nameText);
}
private boolean distBoxIsEmpty()
{
    return distBox.getText().equals("") || distBox.getText().equals(distText);
}
private boolean skladBoxIsEmpty()
{
    return skladBox.getText().equals("") || skladBox.getText().equals(skladText);
}
    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField add1Field;
    private javax.swing.JTextField add2Field;
    private javax.swing.JTextField add3Field;
    private javax.swing.JButton addButton;
    private javax.swing.JTextField allField;
    private javax.swing.JTextField amountField;
    private javax.swing.JScrollPane boxList;
    private javax.swing.JPopupMenu boxPopup;
    private javax.swing.JTextField cena1Field;
    private javax.swing.JTextField cena2Field;
    private javax.swing.JTextField cena3Field;
    private javax.swing.JComboBox currencyComboBox;
    private javax.swing.JTextArea descrTextField;
    private javax.swing.JDialog dialog;
    private javax.swing.JTextField distBox;
    private javax.swing.JFormattedTextField inDateField;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList list;
    private javax.swing.JTextField nameBox;
    private javax.swing.JTextField nettoField;
    private javax.swing.JTextField procAddField1;
    private javax.swing.JTextField procAddField2;
    private javax.swing.JTextField procAddField3;
    private javax.swing.JTextField skladBox;
    private javax.swing.JFormattedTextField taxField;
    private javax.swing.JComboBox typeBox;
    // End of variables declaration//GEN-END:variables

}